﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;

namespace dhSystemBLL.orderData
{
   public  class MaterialTypeBLL
    {
        private MaterialTypeTableAdapter _MaterialTypeAdapter = null;
        private MaterialTypeTableAdapter MaterialTypeAdapter
        {
            get
            {
                if (_MaterialTypeAdapter == null) _MaterialTypeAdapter = new MaterialTypeTableAdapter();
                return _MaterialTypeAdapter;
            }
        }
        /// <summary>
        /// 查询原材料类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.MaterialTypeDataTable getMaterialTypeAll()
        {
            try
            {
                return MaterialTypeAdapter.GetDataMaterialTypeAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据原材料类别查询原材料类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.MaterialTypeDataTable getMaterialTypeAll(string mType)
        {
            try
            {
                return MaterialTypeAdapter.GetDataMaterialTypeBymType(mType);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据原材料类别查询原材料类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.MaterialTypeDataTable getmaterialTypeBymtypeparent(string mtypeparent)
        {
            try
            {
                return MaterialTypeAdapter.GetDatamaterialTypeBymtypeparent(mtypeparent);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 插入原材料类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int MaterialTypeInsert(MaterialTypeModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                MaterialTypeDAL MaterialType = new MaterialTypeDAL(tn, conn);
                MaterialType.MaterialTypeInsert(row);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个原材料类别已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新原材料类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int MaterialTypeUpdate(MaterialTypeModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                MaterialTypeDAL MaterialType = new MaterialTypeDAL(tn, conn);
                MaterialType.MaterialTypeUpdate (row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除一行原材料类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int MaterialTypeDel(string mType)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                MaterialTypeDAL MaterialType = new MaterialTypeDAL(tn, conn);
                MaterialType.MaterialTypeDel(mType);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
               
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
